package leetcode.L39;

import java.util.ArrayList;
import java.util.List;
class Solution {
    private List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if (candidates.length==0) {
            return res;
        }
        backTracking(candidates, target, new ArrayList<>(),0);
        return res;
    }

    protected void backTracking(int[] candidates, int target,List<Integer> currRes,int start) {
        //关键是找到终止条件
        if (target < 0) {
            return;
        }
        if (target==0) {
            res.add(new ArrayList<>(currRes));
        }
        for (int i = start; i < candidates.length; i++) {
            currRes.add(candidates[i]);
            backTracking(candidates,target-candidates[i],currRes,i);
            currRes.remove(currRes.size() - 1);
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.combinationSum(new int[]{2, 3, 6, 7}, 7);
    }
}